import Vue from 'vue'
import VueRouter from 'vue-router'

// 1. 先导入需要的路由组件
// import Reg from '@/views/Reg/Reg.vue'
const Reg = () => import(/* webpackPrefetch: true */ '@/views/Reg/Reg.vue')
// import Login from '@/views/Login/Login.vue'
const Login = () => import(/* webpackPrefetch: true */ '@/views/Login/Login.vue')
// import Main from '@/views/Main/Main.vue'
const Main = () => import(/* webpackPrefetch: true */ '@/views/Main/Main.vue')

// 导入子路由对应的组件
// import Home from '@/views/Menus/Home/Home.vue'
const Home = () => import(/* webpackPrefetch: true */ '@/views/Menus/Home/Home.vue')
// import UserInfo from '@/views/Menus/User/UserInfo.vue'
const UserInfo = () => import(/* webpackPrefetch: true */ '@/views/Menus/User/UserInfo.vue')
// import UserAvatar from '@/views/Menus/User/UserAvatar.vue'
const UserAvatar = () => import(/* webpackPrefetch: true */ '@/views/Menus/User/UserAvatar.vue')
// import UserPwd from '@/views/Menus/User/UserPwd.vue'
const UserPwd = () => import(/* webpackPrefetch: true */ '@/views/Menus/User/UserPwd.vue')
// import ArtCate from '@/views/Menus/Article/ArtCate.vue'
const ArtCate = () => import(/* webpackPrefetch: true */ '@/views/Menus/Article/ArtCate.vue')
// import ArtList from '@/views/Menus/Article/ArtList.vue'
const ArtList = () => import(/* webpackPrefetch: true */ '@/views/Menus/Article/ArtList.vue')
// 安装路由插件
Vue.use(VueRouter)

// 把下面的代码粘贴到路由模块中对应的位置，即可防止路由报错的问题
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}

// 2. 在数组中声明路由规则
const routes = [
  // 注册的路由规则
  { path: '/reg', component: Reg },
  // 登录的路由规则
  { path: '/login', component: Login },
  // 后台主页的路由规则
  {
    path: '/',
    component: Main,
    // 路由重定向
    redirect: '/home',
    // 嵌套子路由
    children: [
      // 注意：子路由的 path 不要以 / 开头
      { path: 'home', component: Home },
      { path: 'user-info', component: UserInfo },
      { path: 'user-avatar', component: UserAvatar },
      { path: 'user-pwd', component: UserPwd },
      { path: 'art-cate', component: ArtCate },
      { path: 'art-list', component: ArtList }
    ]
  }
]

const router = new VueRouter({
  routes
})

export default router
